package cn.bo.cloud.common.core.result;

import cn.bo.cloud.common.core.enums.ResultCode;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;

/**
 * 接口返回值结构
 */
public class ResultContent<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    /// 请求id
    private String requestId;
    /// 状态码  0 成功
    private Integer code;
    /// 返回信息描述
    private String message;

    private T result;

    /// 结果状态
    private boolean success;

    public ResultContent() {
    }
    public ResultContent(String requestId, Integer code, String message, T result, boolean success){
        this.requestId = requestId;
        this.code = code;
        this.message = message;
        this.result = result;
        this.success = success;
    }

    public static <T> ResultContent<T> of(){
        return new ResultContent<>();
    }

    public static <T> ResultContent<T> failure() {
        return failure(ResultCode.ERROR.message());
    }
    public static <T> ResultContent<T> failure(String errMsg) {
        return failure(null, ResultCode.ERROR.code(), errMsg);
    }
    public static <T> ResultContent<T> failure(Integer code, String errMsg) {
        return failure(null, code, errMsg);
    }
    public static <T> ResultContent<T> failure(ResultCode resultCode) {
        return failure(null, resultCode.code(), resultCode.message());
    }
    public static <T> ResultContent<T> failure(String requestId, String errMsg) {
        return failure(requestId, ResultCode.ERROR.code(), errMsg);
    }
    public static <T> ResultContent<T> failure(String requestId, Integer code, String errMsg) {
        return new ResultContent<>(requestId, code, errMsg, null, false);
    }



    public static ResultContent<String> success(){
        return success(ResultCode.SUCCESS.message());
    }
    public static <T> ResultContent<T> success(T content) {
        return success(null, content);
    }
    public static <T> ResultContent<T> success(String requestId, T content) {
        return success(requestId, ResultCode.SUCCESS.message(), content);
    }
    public static <T> ResultContent<T> success(String requestId, String message, T content) {
        return success(requestId, ResultCode.SUCCESS.code(), message, content);
    }
    public static <T> ResultContent<T> success(Integer code, String message, T content) {
        return success(null, code, message, content);
    }
    public static <T> ResultContent<T> success(String requestId, Integer code, String message, T content) {
        return new ResultContent<>(requestId, code, message, content, true);
    }



    public String getRequestId() {
        return requestId;
    }

    public ResultContent setRequestId(String requestId) {
        this.requestId = requestId;
        return this;
    }

    public int getCode() {
        return code;
    }

    public ResultContent setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public ResultContent setMessage(String message) {
        this.message = message;
        return this;
    }

    public T getResult() {
        return result;
    }

    public ResultContent setResult(T result) {
        this.result = result;
        return this;
    }

    public boolean isSuccess() {
        return success;
    }

    public ResultContent setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
                .append("requestId", requestId)
                .append("code", code)
                .append("message", message)
                .append("result", result)
                .append("success", success)
                .toString();
    }

    public String toJson(){
        return new ToStringBuilder(ToStringStyle.JSON_STYLE)
                .append("requestId", requestId)
                .append("code", code)
                .append("message", message)
                .append("result", result)
                .append("success", success)
                .toString();
    }
}
